package edu.ncst.baseweb.service.impl.resinfo;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.res.ResInfo;

import edu.ncst.baseweb.dao.Res;
import edu.ncst.baseweb.mapper.ResInfoMapper;
import edu.ncst.baseweb.service.intrer.resinfo.ResManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ResManageImpl implements ResManageService {
    @Autowired
    private ResInfoMapper resInfoMapper;

    public Result<IPage<ResInfo>> list(ResInfo resInfo){
        QueryWrapper<ResInfo> queryWrapper = Wrappers.query();

        //实验名称查询
        if(StringUtils.isNotEmpty(resInfo.getTiliteName())){
            queryWrapper.like(ResInfo.COL_TILITENAME,resInfo.getTiliteName());
        }
        Page<ResInfo> resPage = new Page<>(resInfo.getCurrent(), resInfo.getPageSize());
        Page<ResInfo> resInfoPage = resInfoMapper.selectPage(resPage, queryWrapper);

        return Result.ok(resInfoPage);
    }
    public Result<Object> save(Res res){
        //新建预约
        ResInfo resInfo = new ResInfo();
        resInfo.setId(res.getId());
        resInfo.setTiliteName(res.getTiliteName());
        resInfo.setLocation(res.getLocation());
        resInfo.setEtime(res.getEtime());
        resInfo.setStime(res.getStime());
        resInfo.setPeopleMin(res.getPeopleMin());
        resInfo.setPeopleMax(res.getPeopleMax());
        resInfo.setType(res.getType());

        if(res.getId()==null){
            resInfo.setReleaseTime(new Date());
            resInfo.setEnable(0);//人数未满
            resInfo.setPeople(0);//人数为0
            resInfo.setState(0);//开始状态为已发布
            resInfoMapper.insert(resInfo);
        }
        else{
            resInfoMapper.updateById(resInfo);
        }
        return Result.ok();
    }
    @Override
    public Result<Object> delete(String resId) throws Exception{
        if (resId==null){
            return Result.failure(500, "要删除的数据不存在");
        }
        else{
            resInfoMapper.deleteById(resId);
            return Result.failure(200, "删除成功！");
        }
    }
}
